package com.iwhalecloud.citybrain.flow.platform.manager.application.common.command;

import java.util.Objects;

import org.flowable.engine.common.impl.interceptor.Command;
import org.flowable.engine.common.impl.interceptor.CommandContext;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.form.engine.FormEngineConfiguration;
import org.flowable.form.engine.impl.persistence.entity.FormResourceEntity;

/**
 * @author cheng.huaxing
 * @date 2019-03-01
 */
public class FormResourceFindCmd implements Command<FormResourceEntity> {

    /**
     * 改成实例级别
     */
    private String processInstanceId;

    /**
     * formKey + .form
     */
    private String resourceName;

    /**
     * formId
     */
    private String formResourceId;

    public FormResourceFindCmd(String processInstanceId, String resourceName, String formResourceId) {
        this.processInstanceId = processInstanceId;
        this.resourceName = resourceName;
        this.formResourceId = formResourceId;
    }

    @Override
    public FormResourceEntity execute(CommandContext commandContext) {
        if (Objects.nonNull(formResourceId)) {
            return ((FormEngineConfiguration) CommandContextUtil.getFormEngineConfiguration(commandContext))
                    .getResourceEntityManager()
                    .findById(formResourceId);
        } else {
            return ((FormEngineConfiguration) CommandContextUtil.getFormEngineConfiguration(commandContext))
                    .getResourceEntityManager()
                    .findResourceByDeploymentIdAndResourceName(processInstanceId, resourceName);
        }
    }
}
